package utils

import (
	"github.com/gin-gonic/gin"
	"strings"
)

// GetClientIp 获取客户端的ip地址
func GetClientIp(c *gin.Context) string {
	clientIp := c.ClientIP()
	remoteIp := c.RemoteIP()
	ip := c.Request.Header.Get("X-Forwarded-For")
	if strings.Contains(ip, "127.0.0.1") || ip == "" {
		ip = c.Request.Header.Get("X-Real-Ip")
	}
	if ip == "" {
		ip = "127.0.0.1"
	}
	if remoteIp != "127.0.0.1" {
		ip = remoteIp
	}
	if clientIp != "127.0.0.1" {
		ip = clientIp
	}
	return ip
}
